from django.shortcuts import render
from django.db import transaction
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
import time
from rest_framework import status
from rest_framework import mixins
from cart.models import Cart
from order.models import Order, OrderGoods, Comment
from order.permissions import OrderPermissions
from order.serializers import OrderSerializer, OrderGoodsSerializer, CommentSerializer
from user.models import Addr


# Create your views here.
class OrderView(GenericViewSet,
                mixins.ListModelMixin,
                mixins.RetrieveModelMixin):
    queryset = Order.objects.all()
    serializer_class =OrderSerializer
    permission_classes = [IsAuthenticated,OrderPermissions]
    #指定商品过滤字段
    filterset_fields=['status']

    #对整个视图的操作开启事务
    @transaction.atomic
    def create(self, request, *args, **kwargs):
        """提交订单视图"""
        #获取请求参数
        addr=request.data.get('addr')
        # print(addr)
        #判断收货地址是否存在
        if not Addr.objects.filter(id=addr,user=request.user).exists():
            return Response({'error':'传入收货地址ID有误'},status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        addr_obj=Addr.objects.get(id=addr)
        addr_str=f'{addr_obj.province}{addr_obj.city}' \
                 f'{addr_obj.country}{addr_obj.address} {addr_obj.name} {addr_obj.phone}'

        # 获取购物车中选中的商品
        cart_goods = Cart.objects.filter(user=request.user, is_checked=True)
        # print(cart_goods)
        if not cart_goods.exists():
            return Response({'error': '订单提交失败，未选中商品'}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        # 生成一个订单编号:时间戳+用户ID
        order_code = str(int(time.time())) + str(request.user.id)

        #设置事务保存节点，（这里要操作数据库）
        save_id=transaction.savepoint()

        try:
            #创建订单
            order=Order.objects.create(user=request.user,
                                     addr=addr_str,
                                     order_code=order_code,
                                     amount=0)
            #保存商品总价
            amount=0
            #遍历购物车中选中的所有商品
            for cart in cart_goods:
                #获取购物车商品数量
                num=cart.number
                #获取购物车商品价格
                price=cart.goods.price
                #将价格进行累加
                amount+=price*num
                # print(cart,cart.number)
                #判断库存
                if cart.goods.stock>num:
                    cart.goods.stock-=num
                    cart.goods.sales+=num
                    cart.goods.save()
                else:
                    # 出错了，事务回滚
                    transaction.savepoint_rollback(save_id)
                    return Response({'error': '创建失败，商品{}库存不足'.format(cart.goods.title)}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)

                #在订单商品表中新增一条数据
                OrderGoods.objects.create(order=order,
                                          goods=cart.goods,
                                          price=price,
                                          number=num)
                #删除购物车中该商品记录
                cart.delete()
            #修改订单的金额
            order.amount=amount
            order.save()
        except Exception as e:
            #出错了，事务回滚
            transaction.savepoint_rollback(save_id)
            return Response({'error': '创建失败，服务器异常'},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        else:
            #提交事务保存到数据库
            transaction.savepoint_commit(save_id)
            #返回结果
            ser=self.get_serializer(order)
        return Response(ser.data,status=status.HTTP_201_CREATED)

    #获取订单列表
    def list(self, request, *args, **kwargs):
        """获取订单列表"""
        queryset = self.filter_queryset(self.get_queryset())
        queryset=queryset.filter(user=request.user)
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

    # 获取单个订单
    def retrieve(self, request, *args, **kwargs):
        #获取订单对象
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        #获取订单中的商品信息
        # print(instance)
        goods=OrderGoods.objects.filter(order=instance)
        #对订单中的商品信息进行序列化
        order_goods=OrderGoodsSerializer(goods,many=True)
        #将订单中的商品信息一起返回
        result=serializer.data
        result['goods_list']=order_goods.data
        return Response(result)

    #关闭订单
    def close_order(self, request, *args, **kwargs):
        #获取到订单对象
        obj=self.get_object()
        #校验订单是否处于未支付状态
        if obj.status !=1:
            return Response({'error':'只能取消未支付订单'},status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        #将订单状态改为关闭
        obj.status=6
        #保存
        obj.save()
        return Response({'message':'订单以关闭'})

class CommentView(GenericViewSet,
                  mixins.CreateModelMixin,
                  mixins.ListModelMixin):
    """商品评价接口"""
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer
    permission_classes = [IsAuthenticated]
    #配置查询评论评价过滤参数
    filterset_fields=['goods','order']

    @transaction.atomic
    def create(self, request, *args, **kwargs):

        #获取参数
        order=request.data.get('order')
        #校验订单编号是否为空
        if not order:
            return Response({"error":'订单ID不能为空'},status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        #订单是否存在，并订单处于待评价状态
        if not Order.objects.filter(id=order).exists():
            return Response({"error":'订单ID有误'},status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        order_obj=Order.objects.get(id=order)
        if order_obj.user !=request.user:
            return Response({'error':'没有评论该订单权限'},status=status.HTTP_403_FORBIDDEN)
        if order_obj.status !=4:
            return Response({'error': '该订单不处于待评论状态'}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)

        #获取订单评论详情参数
        comment=request.data.get('comment')
        if not isinstance(comment,list):
            return Response({'error':'订单评论参数comment格式有误'}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        save_id=transaction.savepoint()
        try:
            for item in comment:
                #遍历参数中的商品评论信息
                #校验参数是否有误
                if isinstance(item,dict):
                    return Response({'error': '订单评论参数comment格式有误'}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
                #获取当前这条评论信息的商品id
                goods=item.get('goods',None)
                if not OrderGoods.objects.filter(order=order_obj,goods_id=goods).exists():
                    return Response({'error': '订单中没有id为{}该商品'.format(goods)}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
            #往item 添加id和用户ID
                item['user']=request.user.id
                item['goods']=goods

            #创建一条评价信息
                ser=CommentSerializer(data=item)
                ser.is_valid()
                ser.save()
            # 修改订单状态已完成
            order.status = 5
            order.save()
        except Exception as e:
            #事务回滚
            transaction.savepoint_rollback(save_id)
        else:
            #提交事务
            transaction.savepoint_commit(save_id)
            return Response({"message":'评论成功'},status=status.HTTP_201_CREATED)


















